*********************************
*****Analysis
*********************************

clear all

cd "$directory"

use "02_ProcessedData\Data_allstudies.dta"

gen data_code=1 if data=="Nigeria1"
replace data_code=2 if data=="Nigeria2"
replace data_code=3 if data=="Honduras"
replace data_code=4 if data=="Andalusia"

lab def base 1 "Nigeria 1" 2 "Nigeria 2"  3 "Honduras" 4 "Andalusia"
lab val data_code base

*nested probabilities

gen difq6q5=pr6-pr5

*We use the absolute mean error. 


gen abserr_pr1=abs(pr1-20) if pr1!=.
gen abserr_pr2=abs(pr2-10) if pr2!=.
gen abserr_pr3=abs(pr3-0) if pr3!=.
gen abserr_pr4=abs(pr4-100) if pr4!=.

lab var abserr_pr1 "Absolute error in q1"
lab var abserr_pr2 "Absolute error in q2"
lab var abserr_pr3 "Absolute error in q3"
lab var abserr_pr4 "Absolute error in q4"

gen violq5q6=(pr5>pr6) if pr6!=. & pr5!=.

lab var violq5q6 "=1 if q5 is higher than q6, and 0 otherwise"


****************************************************
* TABLE 1 - SUMMARY STATISTICS BY SAMPLE
****************************************************
cd "03_Output\"


bysort data: summarize age female heduc


eststo clear

* PANEL 1: Nigeria 1 
estpost summarize age female heduc if data_code==1
eststo Nigeria1

* PANEL 2: Nigeria 2 
estpost summarize age female heduc if data_code==2
eststo Nigeria2

* PANEL 3: Honduras 
estpost summarize age female heduc if data_code==3
eststo Honduras

* PANEL 4: Spain 
estpost summarize age female heduc if data_code==4
eststo Spain

*Generation of the table in latex
* Study 1
esttab Nigeria1 using "table1.tex", ///
    cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)) min(fmt(0)) max(fmt(0))") ///
     nonumber noobs replace ///
    title("Summary statistics by sample") ///
    prehead("\n\\begin{table}[H]\n\\centering\n\\caption{Summary statistics by sample}\n\\begin{tabular}{lrrrrr}\n\\hline\n\\textbf{Variable} & \\textbf{\# obs} & \\textbf{Mean} & \\textbf{Std. Dev.} & \\textbf{Min} & \\textbf{Max} \\\\\n\\hline\n\\multicolumn{6}{l}{\\textit{Study 1: Nigeria 1}} \\\\")

* Study 2
esttab Nigeria2 using "table1.tex", ///
    cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)) min(fmt(0)) max(fmt(0))") ///
     nonumber noobs append ///
    prehead("\n\\multicolumn{6}{l}{\\textit{Study 2: Nigeria 2}} \\\\")

* Study 3
esttab Honduras using "table1.tex", ///
    cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)) min(fmt(0)) max(fmt(0))") ///
     nonumber noobs append ///
    prehead("\n\\multicolumn{6}{l}{\\textit{Study 3: Honduras}} \\\\")

* Study 4
esttab Spain using "table1.tex", ///
    cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)) min(fmt(0)) max(fmt(0))") ///
     nonumber noobs append ///
    prehead("\n\\multicolumn{6}{l}{\\textit{Study 4: Spain}} \\\\") ///
    postfoot("\n\\hline\n\\end{tabular}\n\\end{table}")



****************************************************
* TABLA 2 - BALANCE BEADS vs SLIDER 
****************************************************	

local vars age female heduc
local first = 1

* Creamos macro para ir guardando los asteriscos en paralelo
local starlist

foreach study in 1 2 3 4 {
    foreach var of local vars {

        * Conteos
        qui count if data_code==`study' & slider==0
        local nB = r(N)
        qui count if data_code==`study' & slider==1
        local nS = r(N)

        * Regresión para medias y diferencia
        quietly reg `var' slider if data_code==`study'
        local meanB = _b[_cons]
        local diff  = _b[slider]
        local meanS = `meanB' + `diff'

        * p-valor
        matrix b = r(table)
        local pval = b[4,1]

        * Asteriscos SOLO para p<0.01 y p<0.05
        local stars ""
        if `pval' < 0.01 local stars "***"
        else if `pval' < 0.05 local stars "**"

        * Guardamos la fila numérica en la matriz
        if `first' {
            matrix T2 = (`study', `nB', `nS', `meanB', `meanS', `diff', `pval')
            local first = 0
        }
        else {
            matrix T2 = T2 \ (`study', `nB', `nS', `meanB', `meanS', `diff', `pval')
        }

        * Guardamos los asteriscos en orden en un macro paralelo
        local starlist `starlist' "`stars'"
    }
}

matrix colnames T2 = Study nB nS MeanB MeanS Diff pval
matrix list T2


*Export to latex

file open myfile using "table2.tex", write replace

file write myfile "\begin{table}[H]" _n
file write myfile "\centering" _n
file write myfile "\caption{Statistical balance between Beads and Slider samples}" _n
file write myfile "\begin{tabular}{lrrrrrr}" _n
file write myfile "\hline" _n
file write myfile "\textbf{Variable} & \#obs B & \#obs S & Mean B & Mean S & Diff & p-value \\\\" _n
file write myfile "\hline" _n

local row = 1

foreach study in 1 2 3 4 {

    * Escribimos encabezado del panel
    if `study'==1 file write myfile "\multicolumn{7}{l}{\textit{Study 1: Nigeria 1}} \\\\" _n
    if `study'==2 file write myfile "\multicolumn{7}{l}{\textit{Study 2: Nigeria 2}} \\\\" _n
    if `study'==3 file write myfile "\multicolumn{7}{l}{\textit{Study 3: Honduras}} \\\\" _n
    if `study'==4 file write myfile "\multicolumn{7}{l}{\textit{Study 4: Spain}} \\\\" _n

    * 3 variables por panel
    foreach var in age female heduc {
        * datos de la fila correspondiente
        local nB    = T2[`row',2]
        local nS    = T2[`row',3]
        local meanB = T2[`row',4]
        local meanS = T2[`row',5]
        local diff  = T2[`row',6]
        local pval  = T2[`row',7]

        * asteriscos correctos según p
        local stars ""
        if `pval' < 0.01 local stars "***"
        else if `pval' < 0.05 local stars "**"

        * nombre bonito de la variable
        if "`var'"=="age"    local varlabel "Age"
        if "`var'"=="female" local varlabel "Female"
        if "`var'"=="heduc"  local varlabel "High school or more"

        * escribimos la fila
        file write myfile ///
        "`varlabel' & `nB' & `nS' & " %4.2f (`meanB') " & " %4.2f (`meanS') " & " %4.2f (`diff') " & " %4.3f (`pval') "`stars' \\\\" _n

        local row = `row' + 1
    }

    file write myfile "\\" _n
}

file write myfile "\hline" _n
file write myfile "\end{tabular}" _n
file write myfile "\end{table}" _n
file close myfile



****************************************************
* TABLA 3 - Difference in means, SD and distributions
****************************************************

local vars pr1 pr2 pr3 pr4 difq6q5
local first = 1

foreach study in 1 2 3 4 {
    foreach var of local vars {

        qui count if data_code==`study'
        local N = r(N)

        qui summarize `var' if data_code==`study' & slider==0
        local meanB = r(mean)
        local sdb   = r(sd)

        qui summarize `var' if data_code==`study' & slider==1
        local meanS = r(mean)
        local sds   = r(sd)

        local diff = `meanB' - `meanS'

        * Test de medias (reg)
        qui reg `var' slider if data_code==`study'
        matrix b = r(table)
        local p_mean = b[4,1]

        * Test de varianza (tomamos mínimo p-value)
        qui sdtest `var' if data_code==`study', by(slider)
        local p_var = min(r(p), r(p_l), r(p_u))

        * Test de distribución (ranksum)
        qui ranksum `var' if data_code==`study', by(slider)
        local p_kw = r(p)

        * Creamos matriz con todos los números
        if `first' {
            matrix T3 = (`study', `N', `meanB', `meanS', `diff', `p_mean', `sdb', `sds', `sdb'/`sds', `p_var', `p_kw')
            local first = 0
        }
        else {
            matrix T3 = T3 \ (`study', `N', `meanB', `meanS', `diff', `p_mean', `sdb', `sds', `sdb'/`sds', `p_var', `p_kw')
        }
    }
}

matrix colnames T3 = Study Obs MeanB MeanS Diff p_mean SDb SDs Ratio p_var p_kw
matrix list T3

*Export to latex

capture file close myfile
file open myfile using "table3.tex", write replace

file write myfile "\begin{table}[!h]" _n
file write myfile "  \centering" _n
file write myfile "  \caption{Difference in means, standard deviations and distributions}" _n
file write myfile "\begin{tabular}{lcccccccccccc}" _n
file write myfile "\hline" _n
file write myfile "          & \multicolumn{5}{c}{i) Mean difference test}          &        & \multicolumn{4}{c}{ii) Variance ratio test} &  & iii) KW test \\\\" _n
file write myfile "          & \$obs.\$ & \$m_B\$ & \$m_S\$ & \$m_B\$-\$m_S\$ & \$p\$  &        & \$SD_B\$   & \$SD_S\$   & \$SD_B\$/\$SD_S\$   & \$p\$   &  & \$p\$          \\\\ \cline{2-6} \cline{8-11} \cline{13-13}" _n
file write myfile "\hline" _n

local varlabels "Q1 Q2 Q3 Q4 Q6 - Q5"
local row = 1

foreach study in 1 2 3 4 {

    if `study'==1 file write myfile "\multicolumn{13}{l}{\textbf{\textit{Study 1: Nigeria 1}}} \\\\" _n
    if `study'==2 file write myfile "\multicolumn{13}{l}{\textbf{\textit{Study 2: Nigeria 2}}} \\\\" _n
    if `study'==3 file write myfile "\multicolumn{13}{l}{\textbf{\textit{Study 3: Honduras}}} \\\\" _n
    if `study'==4 file write myfile "\multicolumn{13}{l}{\textbf{\textit{Study 4: Spain}}} \\\\" _n

    forvalues i = 1/5 {
        local obs    = T3[`row',2]
        local meanB  = T3[`row',3]
        local meanS  = T3[`row',4]
        local diff   = T3[`row',5]
        local p_mean = T3[`row',6]
        local sdb    = T3[`row',7]
        local sds    = T3[`row',8]
        local ratio  = T3[`row',9]
        local p_var  = T3[`row',10]
        local p_kw   = T3[`row',11]

        * etiqueta de variable
        local varlbl : word `i' of `varlabels'

        file write myfile ///
        "`varlbl' & `obs' & " %5.2f (`meanB') " & " %5.2f (`meanS') " & " %5.2f (`diff') " & " %4.3f (`p_mean') " & & " %5.2f (`sdb') " & " %5.2f (`sds') " & " %4.2f (`ratio') " & " %4.3f (`p_var') " &  & " %4.3f (`p_kw') " \\\\" _n

        local row = `row' + 1
    }
    file write myfile "\hline" _n
}

file write myfile "\multicolumn{13}{p{7.0in}}{Note: \$m_B\$ and \$m_S\$ refer to the mean of the outcome variable with Beads and Slider respectively, while \$SD_B\$ and \$SD_S\$ refer to the standard deviation for means, variances and distribution. Column iii) refers to the Kruskal–Wallis test (KW test).} \\\\" _n

file write myfile "\end{tabular}" _n
file write myfile "\end{table}" _n

file close myfile
	

********************************************************************************
*Figures 2 to 5: Cumulative Distributions of Elicited Probabilities
********************************************************************************
	
	
*Q1


forvalues k=1(1)4 {
preserve
keep if data_code==`k'
gen pr1_slider=pr1/10 if slider==1
gen pr1_slider2=round(pr1_slider) if slider==1

gen pr1disc=pr1
replace pr1disc=pr1_slider2*10 if slider==1


cumul pr1disc if slider==1, gen(cdf_pr1_slider) 
cumul pr1disc if slider==0, gen(cdf_pr1_beads) equal

collapse  (max) cdf_pr1_slider cdf_pr1_beads, by(pr1disc data_code)
lab def base2 1 "Study 1" 2 "Study 2"  3 "Study 3" 4 "Study 4"
lab val data_code base2

tsset pr1disc
gen cdf_pr1_slider3=l10.cdf_pr1_slider
replace cdf_pr1_slider=cdf_pr1_slider3 if cdf_pr1_slider==.

gen cdf_pr1_beads3=l10.cdf_pr1_beads
replace cdf_pr1_beads=cdf_pr1_beads3 if cdf_pr1_beads==.

drop cdf_pr1_beads3 cdf_pr1_slider3

replace cdf_pr1_beads=cdf_pr1_beads*100
replace cdf_pr1_slider=cdf_pr1_slider*100


gen diff=cdf_pr1_beads-cdf_pr1_slider

local v : label (data_code) `k'
display "`k' is `v'"

twoway  ///
    (con cdf_pr1_slider pr1disc, sort msymbol(C) msize(medium) mcolor(gs6%80) ///
        yaxis(1) lcolor(gs6%80) ylabel(0(20)100, nog labsize(medlarge)) ) ///
    (con cdf_pr1_beads pr1disc, sort xtitle("") msymbol(T) msize(medium) mcolor(gs1%80) ///
        yaxis(1) ylabel(0(20)100, nog labsize(medium)) lcolor(gs1%80) ///
        xlabel(0(10)100, nog labsize(medium)) ///
        xline(20, lcolor(red)) ///
        ytitle("Cumulative (%)", size(medium)) ) ///
    (bar diff pr1disc, vert color(gs4%50) barw(4) yaxis(2) yline(0, axis(2) lcolor(black)) ///
        ytitle("Diff(beads - slider)", axis(2) size(medium)) ///
        ylabel(-30(10)20, axis(2) nog labsize(medium)) ) ///
    , ///
    legend(label(1 Slider) label(2 Beads) label(3 Diff(beads-slider)) pos(6) col(3) size(medium)) ///
    subtitle("`v'") ///
    name(CDpr1_data`k', replace)

restore
}

grc1leg2 CDpr1_data1 CDpr1_data2 CDpr1_data3 CDpr1_data4, col(2) legendfrom(CDpr1_data1) labsize(medium) commonscheme name(figure2, replace ) span 

graph save "figure2" "figure2.gph", replace
graph export "figure2.png", as(png) name("figure2") replace

*Q2

forvalues k=1(1)4 {
preserve
keep if data_code==`k'
gen pr2_slider=pr2/10 if slider==1
gen pr2_slider2=round(pr2_slider) if slider==1

gen pr2disc=pr2
replace pr2disc=pr2_slider2*10 if slider==1


cumul pr2disc if slider==1, gen(cdf_pr2_slider) 
cumul pr2disc if slider==0, gen(cdf_pr2_beads) equal

collapse  (max) cdf_pr2_slider cdf_pr2_beads, by(pr2disc data_code)
lab def base2 1 "Study 1" 2 "Study 2"  3 "Study 3" 4 "Study 4"
lab val data_code base2

tsset pr2disc
gen cdf_pr2_slider3=l10.cdf_pr2_slider
replace cdf_pr2_slider=cdf_pr2_slider3 if cdf_pr2_slider==.

gen cdf_pr2_beads3=l10.cdf_pr2_beads
replace cdf_pr2_beads=cdf_pr2_beads3 if cdf_pr2_beads==.

drop cdf_pr2_beads3 cdf_pr2_slider3

replace cdf_pr2_beads=cdf_pr2_beads*100
replace cdf_pr2_slider=cdf_pr2_slider*100


gen diff=cdf_pr2_beads-cdf_pr2_slider

local v : label (data_code) `k'
display "`k' is `v'"

twoway  ///
    (con cdf_pr2_slider pr2disc, sort msymbol(C) msize(medium) mcolor(gs6%80) ///
        yaxis(1) lcolor(gs6%80) ylabel(0(20)100, nog labsize(medlarge)) ) ///
    (con cdf_pr2_beads pr2disc, sort xtitle("") msymbol(T) msize(medium) mcolor(gs1%80) ///
        yaxis(1) ylabel(0(20)100, nog labsize(medium)) lcolor(gs1%80) ///
        xlabel(0(10)100, nog labsize(medium)) ///
        xline(10, lcolor(red)) ///
        ytitle("Cumulative (%)", size(medium)) ) ///
    (bar diff pr2disc, vert color(gs4%50) barw(4) yaxis(2) yline(0, axis(2) lcolor(black)) ///
        ytitle("Diff(beads - slider)", axis(2) size(medium)) ///
        ylabel(-30(10)20, axis(2) nog labsize(medium)) ) ///
    , ///
    legend(label(1 Slider) label(2 Beads) label(3 Diff(beads-slider)) pos(6) col(3) size(medium)) ///
    subtitle("`v'") ///
    name(CDpr2_data`k', replace)
restore
}


grc1leg2 CDpr2_data1 CDpr2_data2 CDpr2_data3 CDpr2_data4, col(2) legendfrom(CDpr2_data1) labsize(medium) commonscheme name(figure3, replace) span 

graph save "figure3" "figure3.gph", replace
graph export "figure3.png", as(png) name("figure3") replace

*Q3

forvalues k=1(1)4 {
preserve
keep if data_code==`k'
gen pr3_slider=pr3/10 if slider==1
gen pr3_slider2=round(pr3_slider) if slider==1

gen pr3disc=pr3
replace pr3disc=pr3_slider2*10 if slider==1


cumul pr3disc if slider==1, gen(cdf_pr3_slider) 
cumul pr3disc if slider==0, gen(cdf_pr3_beads) equal

collapse  (max) cdf_pr3_slider cdf_pr3_beads, by(pr3disc data_code)
lab def base2 1 "Study 1" 2 "Study 2"  3 "Study 3" 4 "Study 4"
lab val data_code base2

tsset pr3disc
gen cdf_pr3_slider3=l10.cdf_pr3_slider
replace cdf_pr3_slider=cdf_pr3_slider3 if cdf_pr3_slider==.

gen cdf_pr3_beads3=l10.cdf_pr3_beads
replace cdf_pr3_beads=cdf_pr3_beads3 if cdf_pr3_beads==.

drop cdf_pr3_beads3 cdf_pr3_slider3

replace cdf_pr3_beads=cdf_pr3_beads*100
replace cdf_pr3_slider=cdf_pr3_slider*100


gen diff=cdf_pr3_beads-cdf_pr3_slider

local v : label (data_code) `k'
display "`k' is `v'"

twoway  ///
    (con cdf_pr3_slider pr3disc, sort msymbol(C) msize(medium) mcolor(gs6%80) ///
        yaxis(1) lcolor(gs6%80) ylabel(0(20)100, nog labsize(medlarge)) ) ///
    (con cdf_pr3_beads pr3disc, sort xtitle("") msymbol(T) msize(medium) mcolor(gs1%80) ///
        yaxis(1) ylabel(0(20)100, nog labsize(medium)) lcolor(gs1%80) ///
        xlabel(0(10)100, nog labsize(medium)) ///
        xline(0, lcolor(red)) ///
        ytitle("Cumulative (%)", size(medium)) ) ///
    (bar diff pr3disc, vert color(gs4%50) barw(4) yaxis(2) yline(0, axis(2) lcolor(black)) ///
        ytitle("Diff(beads - slider)", axis(2) size(medium)) ///
        ylabel(-30(10)20, axis(2) nog labsize(medium)) ) ///
    , ///
    legend(label(1 Slider) label(2 Beads) label(3 Diff(beads-slider)) pos(6) col(3) size(medium)) ///
    subtitle("`v'") ///
    name(CDpr3_data`k', replace)
	restore
}

grc1leg2 CDpr3_data1 CDpr3_data2 CDpr3_data3 CDpr3_data4, col(2) legendfrom(CDpr3_data1) labsize(medium) commonscheme name(figure4, replace) span 

graph save "figure4" "figure4.gph", replace
graph export "figure4.png", as(png) name("figure4") replace



*Q4

forvalues k=1(1)4 {
preserve
keep if data_code==`k'
gen pr4_slider=pr4/10 if slider==1
gen pr4_slider2=round(pr4_slider) if slider==1

gen pr4disc=pr4
replace pr4disc=pr4_slider2*10 if slider==1


cumul pr4disc if slider==1, gen(cdf_pr4_slider) 
cumul pr4disc if slider==0, gen(cdf_pr4_beads) equal

collapse  (max) cdf_pr4_slider cdf_pr4_beads, by(pr4disc data_code)
lab def base2 1 "Study 1" 2 "Study 2"  3 "Study 3" 4 "Study 4"
lab val data_code base2

tsset pr4disc
gen cdf_pr4_slider3=l10.cdf_pr4_slider
replace cdf_pr4_slider=cdf_pr4_slider3 if cdf_pr4_slider==.

gen cdf_pr4_beads3=l10.cdf_pr4_beads
replace cdf_pr4_beads=cdf_pr4_beads3 if cdf_pr4_beads==.

drop cdf_pr4_beads3 cdf_pr4_slider3

replace cdf_pr4_beads=cdf_pr4_beads*100
replace cdf_pr4_slider=cdf_pr4_slider*100


gen diff=cdf_pr4_beads-cdf_pr4_slider

local v : label (data_code) `k'
display "`k' is `v'"

twoway  ///
    (con cdf_pr4_slider pr4disc, sort msymbol(C) msize(medium) mcolor(gs6%80) ///
        yaxis(1) lcolor(gs6%80) ylabel(0(20)100, nog labsize(medlarge)) ) ///
    (con cdf_pr4_beads pr4disc, sort xtitle("") msymbol(T) msize(medium) mcolor(gs1%80) ///
        yaxis(1) ylabel(0(20)100, nog labsize(medium)) lcolor(gs1%80) ///
        xlabel(0(10)100, nog labsize(medium)) ///
        xline(100, lcolor(red)) ///
        ytitle("Cumulative (%)", size(medium)) ) ///
    (bar diff pr4disc, vert color(gs4%50) barw(4) yaxis(2) yline(0, axis(2) lcolor(black)) ///
        ytitle("Diff(beads - slider)", axis(2) size(medium)) ///
        ylabel(-30(10)20, axis(2) nog labsize(medium)) ) ///
    , ///
    legend(label(1 Slider) label(2 Beads) label(3 Diff(beads-slider)) pos(6) col(3) size(medium)) ///
    subtitle("`v'") ///
    name(CDpr4_data`k', replace)
restore
}

grc1leg2 CDpr4_data1 CDpr4_data2 CDpr4_data3 CDpr4_data4, col(2) legendfrom(CDpr4_data1) labsize(medium) commonscheme name(figure5, replace) span 

graph save "figure5" "figure5.gph", replace
graph export "figure5.png", as(png) name("figure5") replace


********************************************************************************
*Figure 6: Mean Absolute Error between Slider and Beads across Studies
********************************************************************************

*CIBAR

lab def sli 0 "Beads" 1 "Slider" 
lab val slider sli

lab def base2 1 "Study 1" 2 "Study 2"  3 "Study 3" 4 "Study 4"
lab val data_code base2


cibar abserr_pr1, over1(slider) over2(data_code)  bargap(10) graphopts(ylabel(0(10)50, labsize(medium) nogrid) xlabel(, labsize(medium) nogrid)  ytitle(AME) subtitle("{it:Q1}")  legend(pos(6) col(3) size(medium)) name(ame1, replace)) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

cibar abserr_pr2, over1(slider) over2(data_code)  bargap(10) graphopts(ylabel(0(10)50, labsize(medium) nogrid) xlabel(, labsize(medium) nogrid)  ytitle(AME) subtitle("{it:Q2}")  legend(pos(6) col(3) size(medium)) name(ame2, replace)) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

cibar abserr_pr3, over1(slider) over2(data_code)  bargap(10) graphopts(ylabel(0(10)50, labsize(medium) nogrid) xlabel(, labsize(medium) nogrid)  ytitle(AME) subtitle("{it:Q3}")  legend(pos(6) col(3) size(medium)) name(ame3, replace)) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

cibar abserr_pr4, over1(slider) over2(data_code)  bargap(10) graphopts(ylabel(0(10)50, labsize(medium) nogrid) xlabel(, labsize(medium) nogrid)  ytitle(AME) subtitle("{it:Q4}")  legend(pos(6) col(3) size(medium)) name(ame4, replace)) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )


grc1leg2 ame1 ame2 ame3 ame4, col(2) legendfrom(ame1) labsize(medium) commonscheme name(figure6, replace) span 

graph save "figure6" "figure6.gph", replace
graph export "figure6.png", as(png) name("figure6") replace

	
********************************************************************************
*Figure 7: Average difference between Q6 and Q5
********************************************************************************

	
	
forvalues k=1(1)2 {

local v : label (data_code) `k'
display "`k' is `v'"


cibar difq6q5 if data_code==`k', over(slider)  bargap(85) graphopts(ylabel(-5(5)10, labsize(medium) nogrid) yline(0)  ytitle(Difference (Q6-Q5)) subtitle("`v'") xlabel(, labsize(medium) nogrid)  legend(pos(6) col(3) size(medium)) name(g`k', replace)) level(68) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

}


forvalues k=3(1)4 {

local v : label (data_code) `k'
display "`k' is `v'"


cibar difq6q5 if data_code==`k', over(slider)  bargap(85) graphopts(ylabel(-5(5)30, labsize(medium) nogrid) yline(0)  ytitle(Difference (Q6-Q5)) subtitle("`v'") xlabel(, labsize(medium) nogrid)  legend(pos(6) col(3) size(medium)) name(g`k', replace)) level(68) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )
}


grc1leg2 g1 g2 g3 g4, col(2) legendfrom(g1) labsize(medium) commonscheme name(figure7, replace) span 

graph save "figure7" "figure7.gph", replace
graph export "figure7.png", as(png) name("figure7") replace	
	
	

********************************************************************************
*Figure 8:  Proportion of participants violating nested probability properties 
********************************************************************************
	
forvalues k=1(1)4 {

local v : label (data_code) `k'
display "`k' is `v'"


cibar violq5q6 if data_code==`k', over(slider)  bargap(85) graphopts(ylabel(0(0.1)0.3, labsize(medium) nogrid) xlabel(, labsize(medium) nogrid)  ytitle(Ratio (Q5>Q6)) subtitle("`v'")  legend(pos(6) col(3) size(medium)) name(gviol`k', replace)) level(95) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

}


grc1leg2 gviol1 gviol2 gviol3 gviol4, col(2) legendfrom(gviol1) labsize(medium) commonscheme name(figure8, replace) span 

graph save "figure8" "figure8.gph", replace
graph export "figure8.png", as(png) name("figure8") replace


********************************************************************************
*Figure 9:  Proportion of participants violating nested probability properties 
********************************************************************************

forvalues k=1(1)4 {

local v : label (data_code) `k'
display "`k' is `v'"


cibar time_d if data_code==`k', over(slider)  bargap(85) graphopts(ylabel(0(2)10, labsize(medium) nogrid)  xlabel(, labsize(medium) nogrid)  ytitle(Time (in minutes)) subtitle("`v'")  legend(pos(6) col(3) size(medium)) name(gtime`k', replace)) level(95) ciopts(lcolor(black) lwidth(medium)) barcolor(gs1%80 gs6%80 )

}


grc1leg2 gtime1 gtime2 gtime3 gtime4, col(2)  legendfrom(gtime1) labsize(medium) commonscheme name(figure9, replace) span 

graph save "figure9" "figure9.gph", replace
graph export "figure9.png", as(png) name("figure9") replace


********************************************************************************
*Appendices
********************************************************************************
	
***Table A.1: OLS estimation of the effect of slider on the elicited probabilities	

* Encode enumerator code for FE
encode code_enum, gen(code_enum2)

* Dependent variables for columns
local vars pr1 pr2 pr3 pr4 difq6q5

capture erase tableA1.tex
file open myfile using tableA1.tex, write replace

* Write LaTeX header
file write myfile "\begin{table}[H]" _n
file write myfile "\caption{OLS estimation of the effect of slider on the elicited probabilities}" _n
file write myfile "\begin{tabular}{lccccc}" _n
file write myfile "\hline" _n
file write myfile " & (1) & (2) & (3) & (4) & (5) \\" _n
file write myfile " & \$Q1\$ & \$Q2\$ & \$Q3\$ & \$Q4\$ & \$Q6\$-\$Q5\$ \\ \hline" _n

* Define studies
local studies "1 2 3 4 all"

* Loop through studies
foreach s of local studies {

    * Add panel title
    if "`s'"!="all" {
        if "`s'"=="1" local title "Study 1: Nigeria 1"
        if "`s'"=="2" local title "Study 2: Nigeria 2"
        if "`s'"=="3" local title "Study 3: Honduras"
        if "`s'"=="4" local title "Study 4: Spain"
        file write myfile "\multicolumn{6}{l}{\textbf{`title'}} \\" _n
    }
    else {
        file write myfile "\multicolumn{6}{l}{\textbf{All studies}} \\" _n
    }

    * Initialize strings for rows
    local slider_row ""
    local se_row ""
    local cons_row ""
    local cons_se_row ""
    local obs_row ""
    local r2_row ""

    foreach var of local vars {

        * Run regression
        if "`s'"!="all" regress `var' slider heduc age female i.code_enum2 if data_code==`s', r
        if "`s'"=="all" regress `var' slider heduc age female i.code_enum2, r

        * Slider coefficient + stars
        local b_slider = _b[slider]
        local se_slider = _se[slider]
        local p_slider = 2*ttail(e(df_r), abs(`b_slider'/`se_slider'))

        local stars_slider ""
        if `p_slider'<0.01 local stars_slider "***"
        else if `p_slider'<0.05 local stars_slider "**"
        else if `p_slider'<0.10 local stars_slider "*"

        * Format slider values
        local b_slider_fmt = string(`b_slider',"%9.3f")
        local se_slider_fmt = string(`se_slider',"%9.3f")

        * Constant coefficient + stars
        local b_cons = _b[_cons]
        local se_cons = _se[_cons]
        local p_cons = 2*ttail(e(df_r), abs(`b_cons'/`se_cons'))

        local stars_cons ""
        if `p_cons'<0.01 local stars_cons "***"
        else if `p_cons'<0.05 local stars_cons "**"
        else if `p_cons'<0.10 local stars_cons "*"

        * Format constant values
        local b_cons_fmt = string(`b_cons',"%9.3f")
        local se_cons_fmt = string(`se_cons',"%9.3f")

        * Add values to the row strings
        local slider_row "`slider_row' & `b_slider_fmt'`stars_slider'"
        local se_row "`se_row' & (`se_slider_fmt')"

        local cons_row "`cons_row' & `b_cons_fmt'`stars_cons'"
        local cons_se_row "`cons_se_row' & (`se_cons_fmt')"

        local obs = e(N)
        local r2 = string(e(r2),"%4.3f")

        local obs_row "`obs_row' & `obs'"
        local r2_row "`r2_row' & `r2'"
    }

    * Write the panel to LaTeX (single \\ at the end)
    file write myfile "\$slider\$`slider_row' \\" _n
    file write myfile "`se_row' \\" _n
    file write myfile "Constant`cons_row' \\" _n
    file write myfile "`cons_se_row' \\" _n
    file write myfile "Observations`obs_row' \\" _n
    file write myfile "R-squared`r2_row' \\" _n
    file write myfile "\midrule" _n
}

* Footnote
file write myfile "\hline" _n
file write myfile "\multicolumn{6}{p{4.5in}}{Note: All regressions control for age, gender and education level (a dummy = 1 if the subject has completed higher education or more), as well as for enumerator fixed effects. Robust standard errors in parentheses. Asterisks denote significance levels: \$*p<0.10\$, \$**p<0.05\$, \$***p<0.01\$.} " _n

file write myfile "\end{tabular}" _n
file write myfile "\label{tab:ols}%" _n
file write myfile "\end{table}" _n

file close myfile



***Table A.2: OLS estimation of the effect of the slider on accuracy

* Dependent variables for columns
local vars abserr_pr1 abserr_pr2 abserr_pr3 abserr_pr4

capture erase tableA2.tex
file open myfile using tableA2.tex, write replace

* Write LaTeX header
file write myfile "\begin{table}[H]" _n
file write myfile "\caption{OLS estimation of the effect of the slider on accuracy}" _n
file write myfile "\begin{tabular}{lcccc}" _n
file write myfile "\hline" _n
file write myfile " & (1) & (2) & (3) & (4) \\" _n
file write myfile " & MAE \$Q1\$ & MAE \$Q2\$ & MAE \$Q3\$ & MAE \$Q4\$ \\ \hline" _n

* Define studies
local studies "1 2 3 4 all"

* Loop through studies
foreach s of local studies {

    * Add panel title
    if "`s'"!="all" {
        if "`s'"=="1" local title "Study 1: Nigeria 1"
        if "`s'"=="2" local title "Study 2: Nigeria 2"
        if "`s'"=="3" local title "Study 3: Honduras"
        if "`s'"=="4" local title "Study 4: Spain"
        file write myfile "\multicolumn{5}{l}{\textbf{`title'}} \\" _n
    }
    else {
        file write myfile "\multicolumn{5}{l}{\textbf{All studies}} \\" _n
    }

    * Initialize strings for rows
    local slider_row ""
    local se_row ""
    local cons_row ""
    local cons_se_row ""
    local obs_row ""
    local r2_row ""

    foreach var of local vars {

        * Run regression
        if "`s'"!="all" regress `var' slider heduc age female i.code_enum2 if data_code==`s', r
        if "`s'"=="all" regress `var' slider heduc age female i.code_enum2, r

        * Slider coefficient + stars
        local b_slider = _b[slider]
        local se_slider = _se[slider]
        local p_slider = 2*ttail(e(df_r), abs(`b_slider'/`se_slider'))

        local stars_slider ""
        if `p_slider'<0.01 local stars_slider "***"
        else if `p_slider'<0.05 local stars_slider "**"
        else if `p_slider'<0.10 local stars_slider "*"

        * Format slider values
        local b_slider_fmt = string(`b_slider',"%9.3f")
        local se_slider_fmt = string(`se_slider',"%9.3f")

        * Constant coefficient + stars
        local b_cons = _b[_cons]
        local se_cons = _se[_cons]
        local p_cons = 2*ttail(e(df_r), abs(`b_cons'/`se_cons'))

        local stars_cons ""
        if `p_cons'<0.01 local stars_cons "***"
        else if `p_cons'<0.05 local stars_cons "**"
        else if `p_cons'<0.10 local stars_cons "*"

        * Format constant values
        local b_cons_fmt = string(`b_cons',"%9.3f")
        local se_cons_fmt = string(`se_cons',"%9.3f")

        * Add values to the row strings
        local slider_row "`slider_row' & `b_slider_fmt'`stars_slider'"
        local se_row "`se_row' & (`se_slider_fmt')"

        local cons_row "`cons_row' & `b_cons_fmt'`stars_cons'"
        local cons_se_row "`cons_se_row' & (`se_cons_fmt')"

        local obs = e(N)
        local r2 = string(e(r2),"%4.3f")

        local obs_row "`obs_row' & `obs'"
        local r2_row "`r2_row' & `r2'"
    }

    * Write the panel to LaTeX (single \\ at the end)
    file write myfile "\$slider\$`slider_row' \\" _n
    file write myfile "`se_row' \\" _n
    file write myfile "Constant`cons_row' \\" _n
    file write myfile "`cons_se_row' \\" _n
    file write myfile "Observations`obs_row' \\" _n
    file write myfile "R-squared`r2_row' \\" _n
    file write myfile "\midrule" _n
}

* Footnote
file write myfile "\hline" _n
file write myfile "\multicolumn{5}{p{4.5in}}{Note: All regressions control for age, gender and education level (a dummy = 1 if the subject has completed junior high school or more), as well as for enumerator fixed effects. Robust standard errors in parentheses. Asterisks denote significance levels: \$*p<0.10\$, \$**p<0.05\$, \$***p<0.01\$.} " _n

file write myfile "\end{tabular}" _n
file write myfile "\label{tab:ame}%" _n
file write myfile "\end{table}" _n

file close myfile



***Table A.3: OLS estimation of the effect of the slider on the probability of violating nested probability properties


* Dependent variable
local var  violq5q6  

capture erase tableA3.tex
file open myfile using tableA3.tex, write replace

* Write LaTeX header
file write myfile "\begin{table}[H]" _n
file write myfile "\caption{{OLS estimation of the effect of the slider on the probability of violating nested probability properties}" _n
file write myfile "\begin{tabular}{lccccc}" _n
file write myfile "\hline" _n
file write myfile " & (1) & (2) & (3) & (4) & (5) \\" _n
file write myfile " & Study 1 & Study 2 & Study 3 & Study 4 & All studies \\ \hline" _n

* Define studies
local studies "1 2 3 4 all"

* Initialize row strings
local slider_row ""
local se_row ""
local cons_row ""
local cons_se_row ""
local obs_row ""
local r2_row ""
local enum_row ""
local control_row ""
local meanbeads_row ""

* Loop through studies
foreach s of local studies {

    * Run regression (by study or pooled)
    if "`s'"!="all" regress `var' slider heduc age female i.code_enum2 if data_code==`s', r
    if "`s'"=="all" regress `var' slider heduc age female i.code_enum2, r

    * Slider coefficient + stars
    local b_slider = _b[slider]
    local se_slider = _se[slider]
    local p_slider = 2*ttail(e(df_r), abs(`b_slider'/`se_slider'))

    local stars_slider ""
    if `p_slider'<0.01 local stars_slider "***"
    else if `p_slider'<0.05 local stars_slider "**"
    else if `p_slider'<0.10 local stars_slider "*"

    local b_slider_fmt = string(`b_slider',"%9.3f")
    local se_slider_fmt = string(`se_slider',"%9.3f")

    * Constant coefficient + stars
    local b_cons = _b[_cons]
    local se_cons = _se[_cons]
    local p_cons = 2*ttail(e(df_r), abs(`b_cons'/`se_cons'))

    local stars_cons ""
    if `p_cons'<0.01 local stars_cons "***"
    else if `p_cons'<0.05 local stars_cons "**"
    else if `p_cons'<0.10 local stars_cons "*"

    local b_cons_fmt = string(`b_cons',"%9.3f")
    local se_cons_fmt = string(`se_cons',"%9.3f")

    * Collect values
    local slider_row "`slider_row' & `b_slider_fmt'`stars_slider'"
    local se_row "`se_row' & (`se_slider_fmt')"

    local cons_row "`cons_row' & `b_cons_fmt'`stars_cons'"
    local cons_se_row "`cons_se_row' & (`se_cons_fmt')"

    local obs = e(N)
    local r2 = string(e(r2_a),"%4.3f")   // Adjusted R-squared

    local obs_row "`obs_row' & `obs'"
    local r2_row "`r2_row' & `r2'"

    * Enum FE and Controls are always "Yes"
    local enum_row "`enum_row' & Yes"
    local control_row "`control_row' & Yes"

    * Mean beads (mean of dependent variable for slider==0)
    if "`s'"!="all" {
    quietly summarize `var' if slider==0 & data_code==`s'
}
else {
        quietly summarize `var' if slider==0
    }
    local mb = string(r(mean),"%4.3f")
    local meanbeads_row "`meanbeads_row' & `mb'"
}

* Write LaTeX rows
file write myfile "slider`slider_row' \\" _n
file write myfile "`se_row' \\" _n
file write myfile "Constant`cons_row' \\" _n
file write myfile "`cons_se_row' \\" _n
file write myfile "Observations`obs_row' \\" _n
file write myfile "Adjusted R-squared`r2_row' \\" _n
file write myfile "Enum FE`enum_row' \\" _n
file write myfile "Controls`control_row' \\" _n
file write myfile "Mean beads`meanbeads_row' \\" _n

* Footnote
file write myfile "\hline" _n
file write myfile "\multicolumn{6}{p{4.5in}}{Note: All the regressions control for age, gender, and education level (a dummy equal 1 if the subject has completed junior high school or more, and 0 otherwise), as well as for enumerator fixed effects. Robust standard errors in parenthesis. Asterisks denote significance levels: \$*p<0.10\$, \$**p<0.05\$ and \$***p<0.01\$.} " _n

file write myfile "\end{tabular}" _n
file write myfile "\label{tab:violations}%" _n
file write myfile "\end{table}" _n

file close myfile


***Table A.4: OLS estimation of the effect of the slider on time

* Dependent variable
local var time_d

capture erase tableA4.tex
file open myfile using tableA4.tex, write replace

* Write LaTeX header
file write myfile "\begin{table}[H]" _n
file write myfile "\footnotesize" _n
file write myfile "\setlength{\tabcolsep}{5pt}" _n
file write myfile "\renewcommand{\arraystretch}{1.1}" _n
file write myfile "\caption{OLS estimation of the effect of the slider on time}" _n
file write myfile "\begin{tabular}{lcccc}" _n
file write myfile "\hline" _n
file write myfile " & (1) & (2) & (3) & (4) \\" _n
file write myfile " & Study 1 & Study 2 & Study 3 & Study 4 \\ \hline" _n

* Define studies (only 1–4)
local studies "1 2 3 4"

* Initialize row strings
local slider_row ""
local se_row ""
local cons_row ""
local cons_se_row ""
local obs_row ""
local r2_row ""
local enum_row ""
local control_row ""
local meanbeads_row ""

* Loop through studies
foreach s of local studies {

    * Run regression
    regress `var' slider heduc age female i.code_enum2 if data_code==`s', r

    * Slider coefficient + stars
    local b_slider = _b[slider]
    local se_slider = _se[slider]
    local p_slider = 2*ttail(e(df_r), abs(`b_slider'/`se_slider'))

    local stars_slider ""
    if `p_slider'<0.01 local stars_slider "***"
    else if `p_slider'<0.05 local stars_slider "**"
    else if `p_slider'<0.10 local stars_slider "*"

    local b_slider_fmt = string(`b_slider',"%9.3f")
    local se_slider_fmt = string(`se_slider',"%9.3f")

    * Constant coefficient + stars
    local b_cons = _b[_cons]
    local se_cons = _se[_cons]
    local p_cons = 2*ttail(e(df_r), abs(`b_cons'/`se_cons'))

    local stars_cons ""
    if `p_cons'<0.01 local stars_cons "***"
    else if `p_cons'<0.05 local stars_cons "**"
    else if `p_cons'<0.10 local stars_cons "*"

    local b_cons_fmt = string(`b_cons',"%9.3f")
    local se_cons_fmt = string(`se_cons',"%9.3f")

    * Add values to the row strings
    local slider_row "`slider_row' & `b_slider_fmt'`stars_slider'"
    local se_row "`se_row' & (`se_slider_fmt')"

    local cons_row "`cons_row' & `b_cons_fmt'`stars_cons'"
    local cons_se_row "`cons_se_row' & (`se_cons_fmt')"

    local obs = e(N)
    local r2 = string(e(r2),"%4.3f")

    local obs_row "`obs_row' & `obs'"
    local r2_row "`r2_row' & `r2'"

    * Enum FE and Controls are always "Yes"
    local enum_row "`enum_row' & Yes"
    local control_row "`control_row' & Yes"

    * Mean beads (mean of time_d for slider==0)
    quietly summarize `var' if slider==0 & data_code==`s'
    local mb = string(r(mean),"%4.3f")
    local meanbeads_row "`meanbeads_row' & `mb'"
}

* Write LaTeX rows
file write myfile "slider`slider_row' \\" _n
file write myfile "`se_row' \\" _n
file write myfile "Constant`cons_row' \\" _n
file write myfile "`cons_se_row' \\" _n
file write myfile "Observations`obs_row' \\" _n
file write myfile "R-squared`r2_row' \\" _n
file write myfile "Enum FE`enum_row' \\" _n
file write myfile "Controls`control_row' \\" _n
file write myfile "Mean beads`meanbeads_row' \\" _n

* Footnote
file write myfile "\hline" _n
file write myfile "\multicolumn{5}{p{4.5in}}{Note: All the regressions control for age, gender, and education level (a dummy equal 1 if the subject has completed higher education or more, and 0 otherwise), as well as for enumerator fixed effects. Robust standard errors in parenthesis. Asterisks denote significance levels: \$*p<0.10\$, \$**p<0.05\$ and \$***p<0.01\$.} " _n

file write myfile "\end{tabular}" _n
file write myfile "\label{tab:time}%" _n
file write myfile "\end{table}" _n

file close myfile
	

